package org.jeecg.common.util.blockchain;

import org.beykery.eth.WillWallet;
import org.jeecg.common.util.UUIDGenerator;
import org.web3j.abi.datatypes.Address;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.CompletableFuture;
//import io.ipfs.multihash.Multihash;

public class TokenERC721Test {

    public static void main(String[] args) {

       // testDeploy721();
       // test721Balance();
        test721CreateToken();
    }

    public static void test721CreateToken() {
        //    String privateKey = args[0];//这里填入你的eth私钥
        //    String priceString = args[1];//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值
        // 29404234801161523843559422366258777649391763167937715829396789465576954978599
        //0x496f11744bf9af4dd6f9b4f445669131a9413ec4 发布的合约地址
        try {
            String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
            String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2


            WillWallet wallet = new WillWallet(privateKey);
            String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
            Web3j web3j = Web3j.build(new HttpService(url));
            Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
            String clientVersion = web3ClientVersion.getWeb3ClientVersion();
            System.out.println(clientVersion);
            EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
            BigInteger  balance=bl.getBalance();
            BigDecimal  balanceDec=new BigDecimal(balance);
            BigDecimal  balanceResult= balanceDec.divide(new BigDecimal("1000000000000000000")).setScale(5,BigDecimal.ROUND_DOWN);

            System.out.println("账上eth余额 : " + balanceResult);

            BigInteger gwei = new BigInteger("1000000000");
            BigInteger price = gwei.multiply(new BigInteger(priceString));
            BigInteger limit = new BigInteger("4800000");
            ContractGasProvider contractGasProvider = new DefaultGasProvider();
            TokenERC721_sol_ERC721 erc721 = TokenERC721_sol_ERC721.load("0xb4691d45e08f03729d23ac385237c7506c653679", web3j, wallet.getCredentials(), contractGasProvider);
            BigInteger token_uuid=new BigInteger("1");
            RemoteFunctionCall<TransactionReceipt>  transactionReceiptRemoteFunctionCall= erc721.tokenURI(token_uuid);
           // erc721(BigInteger.ONE, hash.toBase58()).send();
            //log.info(String.format("Stored IPFS-URI in ERC-721 NFT: %s", erc721.tokenURI(BigInteger.ONE).send()));

            System.out.println("点赞--->" +  transactionReceiptRemoteFunctionCall.send()+ " <<< .");//0x8ae55d4045838c0ad8d103e16a5c830741238d69
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test721Balance() {
        //    String privateKey = args[0];//这里填入你的eth私钥
        //    String priceString = args[1];//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值
        // 29404234801161523843559422366258777649391763167937715829396789465576954978599
        //0x496f11744bf9af4dd6f9b4f445669131a9413ec4 发布的合约地址
        try {
            String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
            String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2


            WillWallet wallet = new WillWallet(privateKey);
            String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
            Web3j web3j = Web3j.build(new HttpService(url));
            Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
            String clientVersion = web3ClientVersion.getWeb3ClientVersion();
            System.out.println(clientVersion);
            EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
            BigInteger  balance=bl.getBalance();
            BigDecimal  balanceDec=new BigDecimal(balance);
            BigDecimal  balanceResult= balanceDec.divide(new BigDecimal("1000000000000000000"));

            System.out.println("账上eth余额 : " + balanceResult);

            BigInteger gwei = new BigInteger("1000000000");
            BigInteger price = gwei.multiply(new BigInteger(priceString));
            BigInteger limit = new BigInteger("4800000");
            ContractGasProvider contractGasProvider = new DefaultGasProvider();
            TokenERC721_sol_ERC721 erc20 = TokenERC721_sol_ERC721.load("0xb4691d45e08f03729d23ac385237c7506c653679", web3j, wallet.getCredentials(), contractGasProvider);
            CompletableFuture<TransactionReceipt> sendAsync = erc20.totalSupply().sendAsync();

            TransactionReceipt transactionReceipt = sendAsync.get();//返回交易信息
            System.out.println(" totalSupply--->" + transactionReceipt.toString() + " <<< .");//0x8ae55d4045838c0ad8d103e16a5c830741238d69
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 合约地址：0xe258edc72578f569609b6c2ab3cd0d96b2f502ba
     * 721合约地址  0x5ce8c000a050e4f5933f7ae77bc3e78bd5133e7b
     */
    public static void testDeploy721() {
        //    String privateKey = args[0];//这里填入你的eth私钥
        //    String priceString = args[1];//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值
        // 29404234801161523843559422366258777649391763167937715829396789465576954978599
        //0x4abb1cbdeba4fd293c508442c59b3477ac551f1d
        try {
            String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
            String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2


            WillWallet wallet = new WillWallet(privateKey);
            String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
            Web3j web3j = Web3j.build(new HttpService(url));
            Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
            String clientVersion = web3ClientVersion.getWeb3ClientVersion();
            System.out.println(clientVersion);
            EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
            System.out.println("账上eth余额 : " + bl.getBalance());

            BigInteger gwei = new BigInteger("1000000000");
            BigInteger price = gwei.multiply(new BigInteger(priceString));
            BigInteger limit = new BigInteger("4800000");
            ContractGasProvider contractGasProvider = new DefaultGasProvider();
            RemoteCall<TokenERC721_sol_ERC721> call = TokenERC721_sol_ERC721.deploy(web3j, wallet.getCredentials(), contractGasProvider, "珠宝Token", "WBC");//TestToken_sol_TestToken可以改为你自己的合约
            TokenERC721_sol_ERC721 tt = call.send();
            System.out.println("部署721合约 " + tt.getContractAddress() + " 成功 .");//0x8ae55d4045838c0ad8d103e16a5c830741238d69
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
